﻿Imports System.Data.OleDb

Public Class DeleteRecipe
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim prevPage As String = String.Empty
        If Request.Params("ID") = "" Then Response.Redirect("Profile.aspx?User=" & User.Identity.Name)

        If (Not IsPostBack) Then
            prevPage = Request.UrlReferrer.ToString()
        End If

        'Parse the URL to get the value in the Querystring
        Dim getQueryString As String = Request.QueryString("ID")

        'Delete the Gallery Relationship
        Dim delGalleryDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim delGallerySqlString = "DELETE from GALLERY where RECIPE_ID=@f1 "
        Dim delGalleryCommand As OleDbCommand = New OleDbCommand(delGallerySqlString, delGalleryDbConn)
        delGalleryCommand.CommandType = CommandType.Text
        delGalleryCommand.Parameters.AddWithValue("@f1", getQueryString)
        delGalleryDbConn.Open()
        delGalleryCommand.ExecuteNonQuery()
        delGalleryCommand.Dispose()
        delGalleryDbConn.Close()
        delGalleryDbConn.Dispose()

        'Delete the Comment Relationship
        Dim delCommentDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim delCommentSqlString = "DELETE from COMMENT where REC_ID=@f1 "
        Dim delCommentCommand As OleDbCommand = New OleDbCommand(delCommentSqlString, delCommentDbConn)
        delCommentCommand.CommandType = CommandType.Text
        delCommentCommand.Parameters.AddWithValue("@f1", getQueryString)
        delCommentDbConn.Open()
        delCommentCommand.ExecuteNonQuery()
        delCommentCommand.Dispose()
        delCommentDbConn.Close()
        delCommentDbConn.Dispose()

        'Delete the Recipe_Viewed Relationship
        Dim delRecViewedDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim delRecViewedSqlString = "DELETE from RECIPEVIEWED where RECIPE_ID=@f1 "
        Dim delRecViewedCommand As OleDbCommand = New OleDbCommand(delRecViewedSqlString, delRecViewedDbConn)
        delRecViewedCommand.CommandType = CommandType.Text
        delRecViewedCommand.Parameters.AddWithValue("@f1", getQueryString)
        delRecViewedDbConn.Open()
        delRecViewedCommand.ExecuteNonQuery()
        delRecViewedCommand.Dispose()
        delRecViewedDbConn.Close()
        delRecViewedDbConn.Dispose()

        'Delete the Ingredient Relationship
        Dim delIngredientDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim delIngredientSqlString = "DELETE from INGREDIENT where REC_ID=@f1 "
        Dim delIngredientCommand As OleDbCommand = New OleDbCommand(delIngredientSqlString, delIngredientDbConn)
        delIngredientCommand.CommandType = CommandType.Text
        delIngredientCommand.Parameters.AddWithValue("@f1", getQueryString)
        delIngredientDbConn.Open()
        delIngredientCommand.ExecuteNonQuery()
        delIngredientCommand.Dispose()
        delIngredientDbConn.Close()
        delIngredientDbConn.Dispose()

        'Delete the Recipe_Tag Relationship
        Dim delRecipeTagDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim delRecipeTagSqlString = "DELETE from RECIPE_TAG where REC_ID=@f1 "
        Dim delRecipeTagCommand As OleDbCommand = New OleDbCommand(delRecipeTagSqlString, delRecipeTagDbConn)
        delRecipeTagCommand.CommandType = CommandType.Text
        delRecipeTagCommand.Parameters.AddWithValue("@f1", getQueryString)
        delRecipeTagDbConn.Open()
        delRecipeTagCommand.ExecuteNonQuery()
        delRecipeTagCommand.Dispose()
        delRecipeTagDbConn.Close()
        delRecipeTagDbConn.Dispose()

        'Delete the Recipe
        Dim delRecipeDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim delRecipeSqlString = "DELETE from RECIPE where REC_ID=@f1 "
        Dim delRecipeCommand As OleDbCommand = New OleDbCommand(delRecipeSqlString, delRecipeDbConn)
        delRecipeCommand.CommandType = CommandType.Text
        delRecipeCommand.Parameters.AddWithValue("@f1", getQueryString)
        delRecipeDbConn.Open()
        delRecipeCommand.ExecuteNonQuery()
        delRecipeCommand.Dispose()
        delRecipeDbConn.Close()
        delRecipeDbConn.Dispose()

        Response.Redirect(prevPage)

    End Sub

End Class